
import java.util.Stack;

/**
 * Depth-first preorder traversal
 * 1. Visit the root
 * 2. Traverse the left subtree.
 * 3. Traverse the right subtree.
 * 
 * @author Sara Pulis
 */
public class RecursivePreorderTraversal {

    private Stack<BinTree> treeStack = new Stack();

    public RecursivePreorderTraversal() {}

    public RecursivePreorderTraversal(BinTree root) {
        traverse(root);
    }

    private void traverse(BinTree root) {
        treeStack.push(root);
        System.out.print(root.getLabel() + " ");
        if(root.hasLeftKid()) {
            traverse(root.getLeftKid());
        }
        if (root.hasRightKid()) {
            traverse(root.getRightKid());
        }
        treeStack.pop();
    }

}
